package com.seeTools.next.controller.sys;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seeTools.next.api.entity.sys.SysUser;
import com.seeTools.next.api.entity.sys.SysUserRole;
import com.seeTools.next.common.core.PageQuery;
import com.seeTools.next.common.core.Result;
import com.seeTools.next.service.sys.ISysUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 系统用户信息表 前端控制器
 * </p>
 *
 * @author FallenDust
 * @since 2023-04-24
 */
@RestController
@RequestMapping("/sysUser")
public class SysUserController {
    @Resource
    private ISysUserService service;

    /**
     * 分页查询
     *
     * @param pageQuery 查询条件
     * @return 结果
     */
    @SaCheckPermission("user.list")
    @PostMapping("/page")
    public Result<Page<SysUser>> page(@RequestBody PageQuery<SysUser> pageQuery) {
        Page<SysUser> pageInfo = service.pageInfo(pageQuery);
        return Result.success(pageInfo);
    }

    /**
     * 新增
     *
     * @param entity 实体
     * @return Result
     */
    @SaCheckPermission("user.add")
    @PostMapping("/add")
    public Result<Void> add(@RequestBody SysUser entity) {
        return Result.state(service.save(entity));
    }


    /**
     * 更新
     *
     * @param entity 实体
     * @return Result
     */
    @SaCheckPermission("user.edit")
    @PostMapping("/update")
    public Result<Void> update(@RequestBody SysUser entity) {
        return Result.state(service.updateById(entity));
    }

    /**
     * 删除
     *
     * @param entity 实体
     * @return Result
     */
    @SaCheckPermission("user.del")
    @PostMapping("/delete")
    public Result<Void> delete(@RequestBody SysUser entity) {
        return Result.state(service.removeById(entity));
    }


    /**
     * 详情
     *
     * @param id id
     * @return Result
     */
    @GetMapping("/info/{id}")
    public Result<SysUser> info(@PathVariable("id") Long id) {
        SysUser entity = service.getById(id);
        return Result.state(entity);
    }


    /**
     * 密码重置
     *
     * @param entity 用户信息
     * @return Result
     */
    @PostMapping("/resetPassword")
    public Result<Void> resetPassword(@RequestBody SysUser entity) {
        if (entity == null || entity.getId() == null) {
            return Result.error("参数不完整[id]");
        }
        service.resetPassword(entity.getId());
        return Result.success("密码重置成功！");
    }

    /**
     * 分配角色
     *
     * @param sysUser 用户信息
     * @return Result
     */
    @PostMapping("/assignRoles")
    public Result<Void> assignRoles(@RequestBody SysUser sysUser) {
        if (sysUser.getId() == null || sysUser.getRoleIdList() == null) {
            return Result.error("参数不完整[id]");
        }
        service.assignRoles(sysUser.getId(), sysUser.getRoleIdList());
        return Result.success();
    }

    /**
     * 获取用户角色关联列表
     *
     * @param userId
     * @return
     */
    @GetMapping("/getUserRoleList/{userId}")
    public Result<List<SysUserRole>> getUserRoleList(@PathVariable("userId") Long userId) {
        return Result.state(service.getUserRoleList(userId));
    }

    /**
     * 获取所有的用户信息
     *
     * @return
     */
    @PostMapping("/all")
    public Result<List<SysUser>> findAll() {
        return Result.state(service.findAll());
    }
}
